package top.ashman.demo.springcloud.feign.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import top.ashman.demo.springcloud.feign.client.eurekaclient.EurekaClientFeignClient;
import top.ashman.demo.springcloud.feign.client.hystrix.HystrixFeignClient;

/**
 * @author sunzhaojie
 * @date 2018-12-05
 */
@RestController
@RequestMapping("/my")
public class MyController {
    private final EurekaClientFeignClient eurekaClientFeignClient;
    private final HystrixFeignClient hystrixFeignClient;

    public MyController(EurekaClientFeignClient eurekaClientFeignClient, HystrixFeignClient hystrixFeignClient) {
        this.eurekaClientFeignClient = eurekaClientFeignClient;
        this.hystrixFeignClient = hystrixFeignClient;
    }

    @HystrixCommand(groupKey = "myGroup", commandKey = "myCommand")
    @GetMapping("/hello")
    public Mono<String> hello() {
        return Mono.just(eurekaClientFeignClient.hello());
    }

    @GetMapping("/sleep/{seconds}")
    public Mono<String> sleepAndHello(@PathVariable Long seconds) throws InterruptedException {
        // noinspection BlockingMethodInNonBlockingContext
        return Mono.just(eurekaClientFeignClient.sleepAndHello(seconds));
    }

    @GetMapping("/2/hello/{name}")
    public Mono<String> hello2(@PathVariable String name) {
        return Mono.just(hystrixFeignClient.hello(name));
    }

    @GetMapping("/2/sleep/{milliseconds}")
    public Mono<String> sleepAndHello2(@PathVariable Long milliseconds) throws InterruptedException {
        // noinspection BlockingMethodInNonBlockingContext
        return Mono.just(hystrixFeignClient.sleepAndHello(milliseconds));
    }
}
